[] = always returns object of same class[[]] = can extract one element from list or data
framelist_a <- list(a = 2, b = "hallo")
list_a["a"]list_a[["a"]]list_a$a[ selects sub-lists. It always returns a list; if you
use it with a single positive integer, it returns a list of length
one.[[ selects an element within a list.$ is a convenient shorthand: x$y is
equivalent to x[["y"]].drop = FALSE if you are subsetting a matrix, array,
or data frame and you want to preserve the original dimensions. You
should almost always use it when subsetting inside a function.if() works with scalars; ifelse() works
with vectors.x is TRUE, y will be
3;FALSE, y will be
NULL;NA the if statement will throw an
error.for loop to use while
instead, and you can rewrite any while loop to use
repeat, but the converses are not true. That means
while is more flexible than for, and
repeat is more flexible than while.if("2") 3
if(2) 3
for(i in 1:3){
print(i)
}
i <- 1
while(i < 4){
print(i)
i <- i + 1
}
i <- 1
repeat{
print(i)
i <- i + 1
if(i > 3) break
}
sq_root <- function(x){
sqrt(x)
}
df_list to one data frame. The
result should be only a single line of code.do.call(rbind.data.frame, df_list)
do.call pass elements of df_list as
arguments to rbind. Itβs equivalent of
rbind(df_list[[1]], df_list[[2]], df_list[[3]], ....., df_list[[length of df_list]]).mtcars[mtcars$cyl == 4, ]mtcars[-c(1:4), ]mtcars[mtcars$cyl <= 5, ]mtcars[mtcars$cyl == 4 | mtcars$cyl == 6, ]df[is.na(df)] <- 0 doNAs with 0 but keeps the
dimensions.df[sample(1:nrow(df)), ]while and a repeat loop doing the
same asfor(i in LETTERS[1:10]){
print(i)
}
## [1] "A"
## [1] "B"
## [1] "C"
## [1] "D"
## [1] "E"
## [1] "F"
## [1] "G"
## [1] "H"
## [1] "I"
## [1] "J"
# while
i <- 0
while (i < 10) {
i = i + 1
print(LETTERS[i])
}
## [1] "A"
## [1] "B"
## [1] "C"
## [1] "D"
## [1] "E"
## [1] "F"
## [1] "G"
## [1] "H"
## [1] "I"
## [1] "J"
# alternative
i <- 1
while (i < 11) {
print(LETTERS[i])
i = i + 1
}
## [1] "A"
## [1] "B"
## [1] "C"
## [1] "D"
## [1] "E"
## [1] "F"
## [1] "G"
## [1] "H"
## [1] "I"
## [1] "J"
# repeat
i <- 0
repeat{
i = i + 1
print(LETTERS[i])
if (i > 9) {
break
}
}
## [1] "A"
## [1] "B"
## [1] "C"
## [1] "D"
## [1] "E"
## [1] "F"
## [1] "G"
## [1] "H"
## [1] "I"
## [1] "J"
# alternative
i <- 1
repeat{
print(LETTERS[i])
i = i + 1
if (i > 10) {
break
}
}
## [1] "A"
## [1] "B"
## [1] "C"
## [1] "D"
## [1] "E"
## [1] "F"
## [1] "G"
## [1] "H"
## [1] "I"
## [1] "J"
for loop doing the same ascount <- 0
repeat{
x <- sample(1:6, 1)
count <- count + 1
if(x == 6) break
}
print(count)
## [1] 4
for (i in 1:100) {
x <- sample(1:6, 1)
count <- i
if(x == 6) break
}
print(count)
## [1] 5
df will not be past to the final call and
fast_lm is the wrong calldf <- mtcars
lin_mod <- function(df){
lm(y ~ . ,data = df)
}
simple_lin_mod <- function(data, y, x){
df <- data[ ,c(y, x)]
names(df) <- c("y", "x")
lin_mod(df)
}
simple_lin_mod(df, "mpg", "cyl")
##
## Call:
## lm(formula = y ~ ., data = df)
##
## Coefficients:
## (Intercept) x
## 37.885 -2.876
Hallo world!
hist(rnorm(1000))